-
Notifications
You must be signed in to change notification settings - Fork 614
fix: Prevent list modification during iteration in BrowserPool #1703
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Both `_identify_inactive_browsers` and `_close_inactive_browsers` methods were modifying their respective lists (`_active_browsers` and `_inactive_browsers`) while iterating over them. This is a classic bug that can cause items to be skipped or raise RuntimeError. The fix iterates over a copy of the list (`list(...)`) while modifying the original, ensuring all items are properly processed. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1703 +/- ##
==========================================
- Coverage 92.49% 92.48% -0.01%
==========================================
Files 157 157
Lines 10489 10489
==========================================
- Hits 9702 9701 -1
- Misses 787 788 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Would this ever cause any problem? I think it is no issue in the current implementation, but it could be a hidden bug if the implementation changes. Not sure if that is good enough justification for the change :-D
|
|
The point is, when you remove an item from a list during iteration, the iterator's index doesn't adjust, and the next item will be skipped. lst = [1, 2, 3, 4]
for x in lst:
print(x)
if x == 2:
lst.remove(x)
print(lst)See e.g. this https://stackoverflow.com/questions/1637807/modifying-list-while-iterating So this is clearly a bug. |
Sure, but that is not the case in our code. The case in our code is that an item can be added to the list during the iteration, so such an item will be missed in the current loop, but since this function runs periodically, it does not matter, as it will be handled next time the function runs. |
Both
_identify_inactive_browsersand_close_inactive_browsersmethods were modifying their respective lists (_active_browsersand_inactive_browsers) while iterating over them. This is a classic bug that can cause items to be skipped or raiseRuntimeError.The fix iterates over a copy of the list (
list(...)) while modifying the original, ensuring all items are properly processed.Co-Authored-By: Claude Opus 4.5 noreply@anthropic.com